package com.yuyh.library.nets.converters;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.orhanobut.logger.Logger;
import com.yuyh.library.nets.entities.ResultEntity;
import com.yuyh.library.nets.exceptions.ResultException;
import com.yuyh.library.utils.Testentity;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.ResponseBody;
import retrofit2.Converter;

/**
 * created by arvin on 16/10/24 17:24
 */
public class GsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
    private final Gson gson;
    private final Type adapter;

    public GsonResponseBodyConverter(Gson gson, Type adapter) {
        this.gson = gson;
        this.adapter = adapter;
    }

    @Override
    public T convert(ResponseBody value) throws IOException {
        String response = value.string();//36324
        try {
            if (response.contains("trans_result")){//翻译的返回
                return gson.fromJson(response, adapter);
            }
            if (response.contains("\"data\":[{")&&response.contains("app_id")){//查询AppList接口使用，无用的东西，浪费感情
                throw new ResultException(0, response);
            }
            Testentity testentity = new Gson().fromJson(response,Testentity.class);
            ResultEntity resultModel = gson.fromJson(response, ResultEntity.class);
            Log.i("-----", "convert: Json:Code="+resultModel.getCode());
            Log.i("info","请求成功返回数据=="+resultModel.getData().toString());
            if (resultModel.getCode() == 0){//微信二维码支付,无用的东西，浪费感情
                if (resultModel.getData() != null) {
                    T o = null  ;
                    try {
                        if (resultModel.getData().toString().contains("imFeedPraises") && resultModel.getData().toString().contains("imFeedComments")){
                            Log.i("-----", "convert: ============注解@Expose会生效===============");
                            /**
                             * 这样使用gson，注解@Expose会生效
                             * 为了服务器返回的id字段也是操碎了心
                             */
                            Gson gson2 = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
                                    .create();
                            o = gson2.fromJson(resultModel.getData(), adapter);
                        }else {
                            Log.i("-----", "convert: ============注解@Expose不会生效===============");
                            o = gson.fromJson(resultModel.getData(), adapter);
                        }

                        return  o;
                    }catch (Exception e){

                    }
                }
                return null;
            }else
            if (resultModel.getCode() == 1) {
                if (resultModel.getData() != null) {
                    if (resultModel.getData().toString().contains("imFeedPraises") && resultModel.getData().toString().contains("imFeedComments")){
                        Log.i("-----", "convert: =======002=====注解@Expose会生效===============");
                        /**
                         * 这样使用gson，注解@Expose会生效
                         * 为了服务器返回的id字段也是操碎了心
                         */
                        Gson gson2 = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
                                .create();
                        return gson2.fromJson(resultModel.getData(), adapter);
                    }else {
                        Log.i("-----", "convert: =======002=====注解@Expose不会生效===============");
                        try {
                            return gson.fromJson(resultModel.getData(), adapter);
                        }catch (Exception e){
                            e.printStackTrace();
                            throw new ResultException(4, response);
                        }

                    }
                }
                return null;
            }else if (resultModel.getCode() == 2 ){
                ResultException resultException = new ResultException(resultModel.getCode(), response);
                JsonElement data = resultModel.getData();
                if(data != null){
                   InfoBean infoBean = gson.fromJson(data, InfoBean.class);
                    if(infoBean != null){
                        resultException.setErrorInfo(infoBean.getInfo());
                    }
                }
                throw resultException;
            }else if (resultModel.getCode()== -1){
                throw new ResultException(resultModel.getCode(), response);
            }else if (resultModel.getCode()==3){
                throw new ResultException(resultModel.getCode(), response);
            }else if (resultModel.getCode()==1004 || resultModel.getCode()==1008){
                throw new ResultException(resultModel.getCode(), response);
            }else if(resultModel.getCode() == 4){
                throw new ResultException(resultModel.getCode(),response);
            }else {
                throw new ResultException(resultModel.getCode(), null==resultModel.getInfo()?"com.yuyh.library.nets.converters.GsonResponseBodyConverter中69行，不识别接口返回的CODE":resultModel.getInfo());
            }
        } finally {
            value.close();
        }
    }

    class InfoBean{

        private String info;

        public String getInfo() {
            return info;
        }

        public void setInfo(String info) {
            this.info = info;
        }
    }
}
